#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__

#include <vector>
#include <pthread.h>
#include <atomic>

//线程池相关类
class ThreadPool {
public:
	ThreadPool();
	~ThreadPool();

	bool Create(int thread_nums);//创建线程
	void StopAll();

	void InMsgRecvQueueAndSignal(char *buf);
	void Call();//调用线程
	int GetRecvMsgQueueCount() {return recv_msg_queue_count_;}

private:
	static void *ThreadFunc(void *thread_data);//新线程回调函数
	void ClearMsgRecvQueue();

	struct ThreadItem {
		pthread_t _Handle;
		ThreadPool *this_;
		bool if_running;
		ThreadItem(ThreadPool *this_):this_(this_),if_running(false) {}
		~ThreadItem(){}
	};

	static pthread_mutex_t pthread_mutex_;
	static pthread_cond_t  pthread_cond_;
	static bool            shut_down_;//线程退出标志
	int                    thread_nums_;
	std::atomic<int>       running_thread_nums_;
	time_t                 last_emg_time_;//上一次线程不够用的时间
	std::vector<ThreadItem *> thread_vector_;//线程容器
	std::list<char *>      msg_recv_queue_;//接收数据的消息队列
	int                    recv_msg_queue_count_;//消息队列的大小

};

#endif
